import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { callNativeFunc } from "@common/saga-helper";
import {
  SAGA_STOCKTAKING_DEL_BARCODE,
  SAGA_STOCKTAKING_DEL_ITEM_BY_ID,
  SAGA_STOCKTAKING_LOAD_BARCODE_LIST,
  SAGA_STOCKTAKING_LOAD_ITEM_LIST,
  SAGA_STOCKTAKING_LOAD_LOC_LIST,
  SAGA_STOCKTAKING_OPS_FINISH,
  SAGA_STOCKTAKING_PUSH_2_SERVER,
} from "./saga-types";
import {
  DEL_STOCKTAKING_BARCODE,
  SET_STOCKTAKING_BARCODE_LIST,
  SET_STOCKTAKING_ITEM_LIST,
  SET_STOCKTAKING_LOC_LIST,
} from "./action-types";

function* loadStocktakingLocListSaga() {
  yield takeEvery(SAGA_STOCKTAKING_LOAD_LOC_LIST, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.StocktakingHandler,
            action: "loadLocList",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_STOCKTAKING_LOC_LIST],
        },
      ],
    });
  });
}

function* loadStocktakingItemListSaga() {
  yield takeEvery(SAGA_STOCKTAKING_LOAD_ITEM_LIST, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.StocktakingOpsHandler,
            action: "loadItemList",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_STOCKTAKING_ITEM_LIST],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* delStocktakingItemByIdSaga() {
  yield takeEvery(SAGA_STOCKTAKING_DEL_ITEM_BY_ID, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.StocktakingOpsHandler,
            action: "delItemById",
            needNonce: false,
            params: action.payload,
          },
        },
      ],
    });
  });
}

function* loadStocktakingBarcodeListSaga() {
  yield takeEvery(SAGA_STOCKTAKING_LOAD_BARCODE_LIST, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.StocktakingOpsHandler,
            action: "loadBarcodeList",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_STOCKTAKING_BARCODE_LIST],
        },
      ],
    });
  });
}

function* delStocktakingBarcodeSaga() {
  yield takeEvery(SAGA_STOCKTAKING_DEL_BARCODE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.StocktakingOpsHandler,
            action: "delBarcode",
            needNonce: false,
            params: action.payload,
          },
          respActions: [DEL_STOCKTAKING_BARCODE],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* finishStocktakingOneLocSaga() {
  yield takeEvery(SAGA_STOCKTAKING_OPS_FINISH, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.StocktakingOpsHandler,
            action: "finish",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* pushStocktaking2ServerSaga() {
  yield takeEvery(SAGA_STOCKTAKING_PUSH_2_SERVER, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.StocktakingHandler,
            action: "push2Server",
            needNonce: true,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

export default [
  loadStocktakingLocListSaga(),
  loadStocktakingItemListSaga(),
  delStocktakingItemByIdSaga(),
  loadStocktakingBarcodeListSaga(),
  delStocktakingBarcodeSaga(),
  finishStocktakingOneLocSaga(),
  pushStocktaking2ServerSaga(),
];
